﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Windows.Forms;
namespace LearnNet.DAL
{
    public class DALHelper
    {
        /// <summary>
        /// 静态数据库接口
        /// </summary>
        public  static DBUtility.DBHelper LearnNetHelper = GetHelper("ConnectionString");

        static DALHelper()
        {
            if (LearnNetHelper ==null)
            {
                LearnNetHelper = GetHelper("ConnectionString");
            }
        }
        /// <summary>
        /// 从Web.config从读取数据库的连接以及数据库类型
        /// </summary>
        private static DBUtility.DBHelper GetHelper(string connectionStringName)
        {
            DBUtility.DBHelper dbHelper = new DBUtility.DBHelper();

            // 从Web.config中读取数据库类型
            string providerName = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName;
            switch (providerName)
            {
                case "System.Data.OracleClient":
                    dbHelper.DatabaseType = DBUtility.DBHelper.DatabaseTypes.Oracle;
                    break;
                case "MySql.Data.MySqlClient":
                    dbHelper.DatabaseType = DBUtility.DBHelper.DatabaseTypes.MySql;
                    break;
                case "System.Data.OleDb":
                    dbHelper.DatabaseType = DBUtility.DBHelper.DatabaseTypes.OleDb;
                    break;
                case "System.Data.SqlClient":
                    dbHelper.DatabaseType = DBUtility.DBHelper.DatabaseTypes.MsSql;
                    break;
                case "System.Data.SQLite":
                    dbHelper.DatabaseType = DBUtility.DBHelper.DatabaseTypes.SQLite;
                    break;
                    
                default:
                    throw new ApplicationException("数据库配置不对！");
                   
            }

            // 从Web.config中读取数据库连接
            switch (dbHelper.DatabaseType)
            {
                case DBUtility.DBHelper.DatabaseTypes.OleDb:
                    dbHelper.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="
                        + System.Windows.Forms.Application.StartupPath.ToString()
                        + System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
                    string str = dbHelper.ConnectionString;
                    break;
                case DBUtility.DBHelper.DatabaseTypes.SQLite:
                    dbHelper.ConnectionString = "Data Source=" + System.Windows.Forms.Application.StartupPath.ToString()
                        + System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
                    break;
                default:
                    dbHelper.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
                    break;
            }

            return dbHelper;
        }

        /// <summary>
        /// 返回dbhelper 设置类型和链接
        /// </summary>
        /// <param name="dbtype"></param>
        /// <param name="connstring"></param>
        /// <returns></returns>
        public static  DBUtility.DBHelper GetHelper(DBUtility.DBHelper.DatabaseTypes dbtype, string connstring)
        {
            DBUtility.DBHelper dbHelper = new DBUtility.DBHelper(dbtype,connstring);
            return dbHelper;
            
        }
    }
}
